package com.fkdx.common.sequence;

import com.fkdx.common.enums.sys.GroupException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 */
@Component
public class BusinessSequeceUtil {
    @Resource
    private Map<String, BusinessSequece> sequenceMap;
    @Resource
    private BusinessSequece defaultSequence;

    /**
     * 带前缀
     * @param sequenceEnu
     * @param prefix 前缀 比如说库区代码、单位代码
     * @return
     */
    public String get(BusinessSequenceEnum sequenceEnu, String prefix) {
        BusinessSequece sequence = null;
        if (sequenceMap != null) {
            sequence = sequenceMap.get(sequenceEnu);
        }
        if (sequence == null) {
            if (defaultSequence != null) {
                return defaultSequence.get(sequenceEnu, prefix);
            } else {
                throw new GroupException("sequence " + sequenceEnu + " undefined!");
            }
        }
        return sequence.get(sequenceEnu, prefix);
    }

    /**
     * 不带前缀
     * @param sequenceEnu
     * @return
     */
    public String get(BusinessSequenceEnum sequenceEnu) {
        BusinessSequece sequence = null;
        if (sequenceMap != null) {
            sequence = sequenceMap.get(sequenceEnu);
        }
        if (sequence == null) {
            if (defaultSequence != null) {
                return defaultSequence.get(sequenceEnu, "");
            } else {
                throw new GroupException("sequence " + sequenceEnu + " undefined!");
            }
        }
        return sequence.get(sequenceEnu, "");
    }
}
